<?php
namespace App\Repos;

use App\Models\Order;
use App\Models\OrderProduct;
use App\Models\OrderProcess;
use App\Models\OrderPoint;
use App\Models\CustomerService;
use App\Models\Product;
use App\Models\Shop;
use App\Services\RedisService;
use App\Services\WechatTpl;
use EasyWeChat;
use function EasyWeChat\Kernel\Support\generate_sign;
use DB;
use Log;
use Carbon\Carbon;

class OrderRepo
{
	// 后台订单分页
	public static function  paginate($where=[],$num=6,$orderBy='id',$orderWay='desc')
	{
		$query = Order::query();
		$list = $query->where($where)
	  				->with('products','address')
	  				->orderBy($orderBy,$orderWay)
	  				->paginate($num); 
	  	return $list;  
	}

	// 老板端订单分页
	public static function  getPages($where=[],$num=6,$orderBy='id',$orderWay='desc')
	{	   
		$query = Order::query();
		
		
		$status = array_pull($where,'status');
		if($status==1) {
			$query->where('status','1');
		}else if($status==2) {
			$query->whereIn('status',['2','3','4']);
		} elseif($status==3){
			$query->where('status','5');
		}elseif($status==4) {
			$query->where('status','100');
		}else{
			$query->whereIn('status',['-3','-4','-9']);
		}
		
	  	$list = $query->where($where)
	  				->with('products','address')
	  				->orderBy($orderBy,$orderWay)
	  				->paginate($num); 
	  	return $list;     
	}

	// 用户端订单分页
	public static function  pages($where=[],$num=6,$orderBy='id',$orderWay='desc')
	{	   
		$query = Order::query();
		
		
		$status = array_pull($where,'status');
		if($status==1) {
			$query->whereIn('status',['0','1','2','3','4','5','-4','-9']);
		}elseif($status==100) {
			$query->where('status','100');
		}
		
	  	$list = $query->where($where)
	  				->with('products','address')
	  				->orderBy($orderBy,$orderWay)
	  				->paginate($num); 
	  	if($list) {
	  		foreach ($list as $k => $v) {
	  			if($v->type_id==2) {

	  				//团购订单商品信息
	  				$product_id = OrderProduct::where('order_id',$v->id)->value('product_id');
	  				$product = Product::where(['id'=>$product_id])
	  				->select(['id','name','spec_id','thumb','origin_price','weight'])	  				
        			->with('spec:id,name')->first();

        			$shop = Shop::select(['id','name','address'])
        						->where(['id'=>$v->shop_id])
        						->first();

	  				$gwhere['ug.code'] = $v->group_code;
	  				$groupUsers = DB::table('user_groupbuy as ug')
						->select(['ug.end_at','u.nickname','u.avatar'])
						->leftJoin('users as u','u.id','=','ug.user_id')
						->where($gwhere)
						->get();
					//Log::info(json_encode($gwhere));
					$pgwhere['ug.code']      = $v->group_code;	
					$pgwhere['ug.parent_id'] = 0;	
					$productGroup = DB::table('user_groupbuy as ug')
						->select(['ug.end_at','p.group_num','p.group_price'])
						->where($pgwhere)						
						->leftJoin('product_groups as p','p.id','=','ug.product_group_id')
						->first();
					//dd($gwhere,$productGroup);

						$end_date = Carbon::parse($productGroup->end_at);
						$now      = Carbon::now();       
            			$minutes  = $now->diffInMinutes($end_date);

            		$group['product'] 	  = $product;	
            		$group['shop']        = $shop;				
					$group['users']       = $groupUsers;
					$group['group_price'] = $productGroup->group_price;
					$group['need_num'] = $productGroup->group_num;	
					$group['user_num'] = $groupUsers->count();
					$group['minutes']  = $minutes;

					$v->group= $group;

	  				
	  			}
	  		}
	  	}			
	  	return $list;     
	}

	public static function find($where)
	{		
		$order = Order::where($where)->with('products','address')->first();
		return $order;
	}

	/**
	 * 创建订单
	 */
	public static function create($data,$products=[])
	{
		$openid        = array_pull($data,'openid');
		$total_money   = (int)$data['total_money'];		

		DB::beginTransaction();
		try {
			// 用了券 那么券就废了
			if(array_key_exists('user_coupon_id', $data)) {
				UserCouponRepo::update(['id'=>$data['user_coupon_id']],['is_used'=>1]);					
			}

			$order    = Order::create($data);
			
			if($products) {
				foreach ($products as $key => $v) {
					
					$v['order_id'] = $order->id;
					$v['shop_id']  = $data['shop_id'];
					OrderProduct::create($v); 
					// 是放购物车
					$cartWhere['user_id']    = $data['user_id'];					
					$cartWhere['shop_id']    = $data['shop_id'];
					$cartWhere['product_id'] = $v['product_id'];
					CartRepo::release($cartWhere);
				}	
			}

			// 如果用了余额要剪掉
			$moneyLog = array_only($data,['user_id','shop_id']);
			$moneyLog['type']  = 2;
			$moneyLog['money'] = $data['user_money'];
			$moneyLog['order_id'] = $order->id;
			//dd($moneyLog);
			$rs = UserMoneyLogRepo::create($moneyLog);
			if($rs['status'] = false) {
				DB::rollBack();
			}

			DB::commit();
		} catch (\Exception $e) {
			dd($e->getMessage());			
			DB::rollBack();
			return false;
		}

		$unifyData['openid']       = $openid;
		$unifyData['total_money'] = $total_money;
		$unifyData['code']         = $data['code'];
		$unifyData['order_id']    = $order->id;
		return self::wechatUnifyOrder($unifyData);
	}

	/**
	 * 创建订单
	 */
	public static function createGoupOrder($data,$product)
	{
		$openid        = array_pull($data,'openid');
		$total_money   = (int)$data['total_money'];		

		DB::beginTransaction();
		try {
			// 用了券 那么券就废了
			if(array_key_exists('user_coupon_id', $data)) {
				UserCouponRepo::update(['id'=>$data['user_coupon_id']],['is_used'=>1]);					
			}

			
					
			// 修改团购状态
			$groupWhere['code']       = $data['group_code'];
			// 根据product_id;查询成团所需人数
			$pg = ProductRepo::group($product['product_id']);

			// 根据code 查 user_group_buy 查目前的人数；
			$gnum = UserGroupbuyRepo::countNum($data['group_code']);


			// 如果目前的人数大于或者等于成团所需人数那么 状态修改为100
			// 如果目前人数小于成团所需人数那么状态为1
			$ugbData['status'] = 1;  
			$data['status']    = 0;  // 默认拼团中
			if(($gnum+1)>=$pg->group_num) {
				$ugbData['status'] = 100;
				$data['status']    = 1; //拼团成功

				// 同团的订单状态改为待确认
				Order::where(['group_code'=>$data['group_code']])->update(['status'=>1]);
			}

			// 更新团购状态
			$groupWhere['product_id'] = $product['product_id'];
			$groupWhere['code']       = $data['group_code'];
			UserGroupbuyRepo::update($groupWhere,$ugbData);

			// 保存订单主体
			$order    = Order::create($data);			
			$product['order_id'] = $order->id;
			$product['shop_id']  = $data['shop_id'];
			OrderProduct::create($product); 

			// 如果用了余额要剪掉
			$moneyLog = array_only($data,['user_id','shop_id','user_id']);
			$moneyLog['type']  = 2;
			$moneyLog['money'] = $data['user_money'];
			$moneyLog['order_id'] = $order->id;
			//dd($moneyLog);
			$rs = UserMoneyLogRepo::create($moneyLog);
			if($rs['status'] = false) {
				DB::rollBack();
			}

			DB::commit();
		} catch (\Exception $e) {
			dd($e->getMessage());			
			DB::rollBack();
			return false;
		}

		return true;
	}


	/**
	 * 确认订单
	 */
	public static function confirm($data)
	{
		$rs['status'] = false;
		$rs['msg']    = '操作失败';
		$order_status = 3;
		
		//先检查订单的状态防止误操作 订单状态在待付款1或者已付款2的才可以确认
		// 因为现在没有线上付款 所以直接改为3
		DB::beginTransaction();
		try{
			$order = Order::find($data['order_id']);

			if($order->status>2||$order->status<1) {
				$rs['msg'] = '订单状态错误';
				return $rs;
			}
			if($order->address_id=='') {
				// 到店自提直接是5
				$order_status = 5;
			}
			$order->status = $order_status;
			$order->save();
			
			$process['order_id']     = $data['order_id'];
			$process['shop_user_id'] = $data['shop_user_id'];
			$process['order_status'] = $order_status;
			$process['remark']       = '卖家确认接单';
			$flag = OrderProcess::create($process);	
			DB::commit();
		} catch(\Exception $e) {
			DB::rollBack();
			$rs['msg'] = $e->getMessage();
			return $rs;
		}
		
		//dd($order);
		if($flag) {
			$rs['status'] = true;
			return $rs;
		}
		return $rs;
		

	}

	/**
	 * 配送订单需要上传订单商品图片
	 */
	public static function orderPic($data)
	{

		$rs['status'] = false;
		$order_status = 4;
		DB::beginTransaction();
		try{
			$order = Order::find($data['order_id']);
			if(empty($order->address_id)) {
				$rs['msg'] = '此订单是自取订单无需上传照片';
				return $rs;
			}
			//配送订单确认后才能上图
			// if($order->status!=3){
			// 	$rs['msg'] = '请先确认订单';
			// 	return $rs;
			// }

			$order->status = $order_status;
			$order->save();

			// 更新订单商品表			
			$picWhere['order_id']   = $data['order_id'];
			$picWhere['product_id'] = $data['product_id'];	
			$pic = $data['pic'];

			OrderProduct::where($picWhere)->update(['pic'=>$pic]);
			


			$process['order_id']     = $data['order_id'];
			$process['shop_user_id'] = $data['shop_user_id'];
			$process['order_status'] = $order_status;
			$process['remark']       = '卖家上传订单商品图片';
			$flag = OrderProcess::create($process);

			DB::commit();
		} catch(\Exception $e) {
			DB::rollBack();
			$rs['msg'] = $e->getMessage();
			return $rs;
		}
		
		if($flag) {
			$rs['status'] = true;
			return $rs;
		}
		return $rs;
	}

	/**
	 * 开始配送
	 */
	public static function dispatch($data)
	{
		$rs['status'] = false;
		$order_status = 5;
		DB::beginTransaction();
		try{
			$order = Order::find($data['order_id']);
			if(empty($order->address_id)) {
				$rs['msg'] = '此订单是自取订单无需配送';
				return $rs;
			}
			//配送订单确认后要上传图才能继续
			if($order->status!=4){
				$rs['msg'] = '请先上传订单商品图片';
				return $rs;
			}

			$order->status = $order_status;
			$order->save();
			$process['order_id']     = $data['order_id'];
			$process['shop_user_id'] = $data['shop_user_id'];
			$process['order_status'] = $order_status;
			$process['remark']       = '卖家已配送';
			$flag = OrderProcess::create($process);	
			DB::commit();
		} catch(\Exception $e) {
			DB::rollBack();
			$rs['msg'] = $e->getMessage();
			return $rs;
		}
		
		if($flag) {
			$rs['status'] = true;
			return $rs;
		}
		return $rs;
		

	}

	/**
	 * 从待付款中付款
	 */
	public static function pay($data)
	{
		$rs['status'] = false;
		$order = Order::where(['code'=>$data['code']])->first();
		if(!$order) {			
			$rs['msg'] = '订单不存在';
			return $rs;
		}

		if($order->status !=1 ){
			$rs['msg'] = '订单状态错误'; 
			return $rs;
		}	

		if($data['total_money'] != $order->total_money) {
			$rs['msg'] = '订单金额错误';
			return $rs;
		}
		$rs['status'] = true;
		$rs['data'] = self::wechatUnifyOrder($data);
		return $rs;

	}

	public static function count($where)
	{
		$user_id   = $where['user_id'];
		$key       = $where['key'];
		//$redis_key = 'fruit:users:order:count:'.$user_id.'_'.$key;
		//$num = RedisService::get($redis_key);
		//if($num) return $num;

		$num    = Order::where(['user_id'=>$user_id])
						->whereIn('status',$where['status'])
						->count();
		//$redis_key = 'fruit:users:order:count:'.$user_id.'_'.$key;
		//$redis_data = (int)$num;				
		//RedisService::set($redis_key,$redis_data);
		return $num;
	}


	public static function cancle($data)
	{
		$rs['status'] = false;
		$where['code'] = $data['code'];	
		$order = Order::where($where)->first();
		if(!$order) {			
			$rs['msg'] = '订单不存在';
			return $rs;
		}
		// if($order->status !=1 ){
		// 	$rs['msg'] = '订单状态不允许取消'; 
		// 	return $rs;
		// }
		DB::beginTransaction();
		try {
			if($order->status==1) {
				// 待确认订单不需要审核
				$order->status = '-1';	
			} else {
				// 其他状态需要 卖家审核
				$order->status = '-3';
			}
			
			$order->save();
			// 恢复优惠券
			if($order->user_coupon_id){
				UserCouponRepo::update(['id'=>$order->user_coupon_id],['is_used'=>0]);
			}

			if($order->user_money) {
				$money['type']    = 4;
				$money['shop_id'] = $order->shop_id;
				$money['user_id'] = $order->user_id;
				$money['money']   = $order->user_money;
				UserMoneyLogRepo::create($money);
			}
			
			DB::commit();
		} catch (\Exception $e) {
			DB::rollBack();
			$rs['msg'] = $e->getMessage();
			return $rs;
		}
		
		$rs['status'] = true;
		return $rs;	
	}

	public static function cancleGroupbuy($where)
	{
		$rs['status'] = false;
		//$where['group_code'],$where['user_id'];
		$order = Order::where($where)->first();
		//dd($where,$order);
		if(!$order) {			
			$rs['msg'] = '订单不存在';
			return $rs;
		}

		$ugbWhere['code']  = $where['group_code'];
		$ugbData['status'] = '-2';
		UserGroupbuyRepo::update($ugbWhere,$ugbData);
		
		DB::beginTransaction();
		try {
			
			// 拼团未成订单不需要审核
			$order->status = '-1';	
			
			
			$order->save();
			// 恢复优惠券
			if($order->user_coupon_id){
				UserCouponRepo::update(['id'=>$order->user_coupon_id],['is_used'=>0]);
			}

			$process['order_id']     = $order->id;
			//$process['shop_user_id'] = $data['shop_user_id'];
			$process['order_status'] = '-1';
			$process['remark']       = '买家参加了新的拼团，取消了之前的拼团';
			$flag = OrderProcess::create($process);	
			
			DB::commit();
		} catch (\Exception $e) {
			DB::rollBack();
			$rs['msg'] = $e->getMessage();
			return $rs;
		}
		
		$rs['status'] = true;
		return $rs;	
	}

	/*拼团过期失效*/
	public static function invildGroupbuy($where)
	{
		$rs['status'] = false;
		
		$order = Order::where(['group_code'=>$where['group_code']])->first();
		
		if(!$order) {			
			$rs['msg'] = '订单不存在';
			return $rs;
		}

		
		
		DB::beginTransaction();
		try {
			
			// 拼团未成订单不需要审核
			$order->status = '-1';	
			$order->save();

			$ugbWhere['id']    = $where['ugb_id'];
			$data['status']    = '-1';
			UserGroupbuyRepo::update($ugbWhere,$data);

			// 恢复优惠券
			if($order->user_coupon_id){
				UserCouponRepo::update(['id'=>$order->user_coupon_id],['is_used'=>0]);
			}

			// 恢复余额
			if($order->user_money) {
				$money['type']    = 4;
				$money['shop_id'] = $order->shop_id;
				$money['user_id'] = $order->user_id;
				$money['money']   = $order->user_money;
				UserMoneyLogRepo::create($money);
			}

			$process['order_id']     = $order->id;
			//$process['shop_user_id'] = $data['shop_user_id'];
			$process['order_status'] = '-1';
			$process['remark']       = '24小时内拼团失败';
			$flag = OrderProcess::create($process);	


			
			DB::commit();
		} catch (\Exception $e) {
			DB::rollBack();
			Log::info('拼团过期操作：'.$e->getMessage());
			$rs['msg'] = $e->getMessage();
			return $rs;
		}

		// 推送
			$msg = $order->toArray();
			$msg['total_money']  = $order->total_money/100;
			$msg['openid']       = object_get($order,'user.openid'); 
			$msg['shop_name']    = object_get($order,'shop.name');
			$msg['product_info'] = self::productInfo($order->id);
			$msg['remark']       = '拼团人数不足，订单取消';
			Log::info('拼团人数不足，订单取消:'.json_encode($msg));
			WechatTpl::sendTplMsg('order_cancle',$msg);
		
		$rs['status'] = true;
		return $rs;	
	}

	public static function wechatUnifyOrder($data)
	{
		$payment = EasyWeChat::payment(); // 微信支付
		$result = $payment->order->unify([
		    'body'         => '幸福壹选订单',
		    'out_trade_no' => $data['code'],
		    'trade_type'   => 'JSAPI',  // 必须为JSAPI
		    'openid'       => $data['openid'], // 这里的openid为付款人的openid
		    'total_fee'    => $data['total_money'], // 总价单位分
		    'timeStamp' => time(),
		]);

		// 如果成功生成统一下单的订单，那么进行二次签名
		if ($result['return_code'] === 'SUCCESS') {
		    // 二次签名的参数必须与下面相同
		    $params = [
		        'appId'     => config('wechat.mini_program.default.app_id'),
		        'timeStamp' => time(),
		        'nonceStr'  => $result['nonce_str'],
		        'package'   => 'prepay_id=' . $result['prepay_id'],
		        'signType'  => 'MD5',
		    ];
		    // config('wechat.payment.default.key')为商户的key
		    $params['paySign'] = generate_sign($params, config('wechat.payment.default.key'));
		    $params['signJs'] = $result['sign'];
		    $params['order_id']      = $data['order_id'];
		    $params['order_code']    = $data['code'];
		    $params['order_created'] = date('Y-m-d H:i:s');
		    return $params;
		} else {
		    return $result;
		}
	}

	/**
	 * 商家扫码方式确认订单完成
	 * 有返现要发现
	 */
	public static function validPay($data)
	{
		$rs['status'] = false;
		$status = 100;
		$where['shop_id'] = $data['shop_id'];
		$where['id']    = $data['order_id'];
		DB::beginTransaction();
		try {
			$order = Order::where($where)->first();
			if($order->status>=100) {
				$rs['msg'] = '订单状态错误';
				return $rs;
			}

			if($order) {
				$order->completed_at = date('Y-m-d H:i:s');
				$order->status = $status;
				$order->save();
			}
			// 店铺是否返现
			$shop = ShopRepo::find(['id'=>$data['shop_id']]);

			if($shop->cashback_rate>0) {
				$products = OrderProduct::where(['order_id'=>$data['order_id']])
										  ->get();
										
				foreach ($products as $k => $v) {
					//产品是否返现
					if(object_get($v,'product.is_cashback')){
						$user_money['shop_id']    = $data['shop_id'];
						$user_money['order_id']   = $data['order_id'];
						$user_money['user_id']    = $order->user_id;
						$user_money['product_id'] = $v->product_id;
						$user_money['type']  = 1;
						$user_money['money'] = $v->total_price*($shop->cashback_rate/100);
						// 保存返现信息；
						UserMoneyLogRepo::create($user_money);
					}
				}	
			}	

			$process['order_id']     = $data['order_id'];
			$process['shop_user_id'] = $data['shop_user_id'];
			$process['order_status'] = $status;
			$process['remark']       = '订单完成';
			$flag = OrderProcess::create($process);
			$rs['status'] = true;
			DB::commit();
		} catch (\Exception $e) {
			DB::rollBack();
			$rs['msg'] = $e->getMessage();
		}

		return $rs;	

	}


	/**
	 * 验证订单拍十秒
	 * @param $where 包含user_id 和 code
	 */
	public static function tenSeconds($where)
	{	
		$rs['status'] = false;
		$result = order::where($where)->first();
		if(empty($result)) {
			$rs['msg'] = '订单错误';
			return $rs;
		}

		if($result->tenseconds == 1) {
			$rs['msg'] = '此订单已经参与过拍十秒';
			return $rs;
		}

		$rs['status'] = true;
		return $rs;
	}

	public static function changeTenSecondStatus($code)
	{
		return Order::where('code',$code)->update(['tenseconds'=>1]);
	}

	public static function total($where)
	{
		$today =  Order::whereDate('created_at', '=', date('Y-m-d'))->sum('total_amount');
		$all = Order::sum('total_amount');
		$data['today'] = $today;
		$data['all'] = $all;
		return $data;
	}

	public static function reject($data)
	{
		$rs['status'] = false;
		$order_status = '-2';
		$where = array_only($data,['id','shop_id']);
		$order = Order::where($where)->first();

		if(empty($order)) {
			$rs['msg'] = '订单不存在';
			return $rs;
		}
		// 只有待确认状态的订单才能被拒单
		if($order->status!=1) {
			$rs['msg'] = '订单当前状态无法拒绝';
			return $rs;
		}

		$order->status = $order_status;
		$flag = $order->save();
		if($flag) {
			$rs['status'] = true;

			// 推送
			$msg = $order->toArray();
			$msg['total_money'] = $order->total_money/100;
			$msg['openid']       = object_get($order,'user.openid'); 
			$msg['shop_name']    = object_get($order,'shop.name');
			$msg['product_info'] = self::productInfo($order->id);
			$msg['remark']       = '暂时不能发货，请谅解';
			Log::info('商家拒绝订单:'.json_encode($msg));
			WechatTpl::sendTplMsg('order_cancle',$msg);

			$process['order_id']     = $data['id'];
			$process['shop_user_id'] = $data['shop_user_id'];
			$process['order_status'] = $order_status;
			$process['remark']       = '卖家拒绝接单';
			$flag = OrderProcess::create($process);	
			return $rs;
		}
		$rs['msg'] = '拒单失败';
		return $rs;
	}

	/**
	 * 同意 用户提出 取消的订单申请
	 */
	public static function approval($data)
	{
		$rs['status'] = false;
		$order_status = $data['status'];
		$where = array_only($data,['id','shop_id']);
		$order = Order::where($where)->first();

		if(empty($order)) {
			$rs['msg'] = '订单不存在';
			return $rs;
		}
		// 只有待确认状态的订单才能被拒单
		if($order->status!=-3) {
			$rs['msg'] = '订单状态错误';
			return $rs;
		}

		$order->status = $order_status;
		$msg = $order_status==90?'卖家同意取消订单':'卖家拒绝取消订单';
		$flag = $order->save();
		if($flag) {
			$rs['status'] = true;

			$process['order_id']     = $data['id'];
			$process['shop_user_id'] = $data['shop_user_id'];
			$process['order_status'] = $order_status;
			$process['remark']       = $msg;
			$flag = OrderProcess::create($process);	
			return $rs;
		}
		$rs['msg'] = '操作失败';
		return $rs;
	}

	



	public static function point($data)
	{
		$rs['status'] = false;
		$rs['msg'] = '评价失败';
		$where['code']    = array_pull($data,'code');
		$where['user_id'] = $data['user_id'];
		DB::beginTransaction();
		try {
			$order = Order::where($where)->first();
			if(!$order) {
				$rs['msg'] = '订单不存在';
				return $rs;
			}

			if($order->status!=100) {
				$rs['msg'] = '付款后才能评价订单';
			}
			$order->status = 200;
			$order->save();


			
			foreach($data['product_id'] as $k=>$p) {
				$op['order_id']   = $order->id;
				$op['user_id'] 	  = $data['user_id'];
				$op['product_id'] = $p;
				$op['point']      = $data['point'][$k];
				$flag = OrderPoint::create($op);
			}
			
			if($flag) {
				DB::commit();
				$rs['status'] = true;
				return $rs;
			}
			DB::rollBack();
			return $rs;
			
		} catch (\Exception $e) {
			DB::rollBack();
			$rs['msg'] = $e->getMessage();
			return $rs;
		}
		
		return $rs;
	} 

	public static function orderCount()
	{
		$num = Order::count();
		return $num;
	}

	public static function orderAmount()
	{
		// $total = Order::sum('total_amount');
		// return $total/100;	
	}

	public static function saveCustomerService($where)
	{
		$rs['status'] = false;
		$order = Order::where($where)->first();
		if($order) {
			$data = $order->toArray();
			$cs = array_only($data,['user_id','shop_id']);
			$cs['order_id'] = $data['id'];
			$flag = CustomerService::create($cs);
			if($flag){
				$order->status = '-9';
				$order->save();
				$rs['status']  = true;
				return $rs;	
			} 
		} else {
			$rs['msg'] = '订单不存在';
		}
		return $rs;
	}

	public static function productInfo($order_id)
	{
		$products_names = '';
		$products = OrderProduct::where(['order_id'=>$order_id])
						->pluck('product_name');
		if($products) {
			$pros = $products->toArray();
			$products_names = implode(',', $pros);
			return $products_names;
		}

		return $product_names;				


	}

	public function customerComplete($data)
	{
		$order = Order::find($data['order_id']);
		$order->status = '-100';
		$flag = $order->save();

		CustomerService::where(['id'=>$data['customerservice_id']])->update(['remark'=>$data['remark']]);
		return $flag;
	}


	

	

	
	
	

	
	
	
}